<?php


namespace App\Helpers;


use App\Models\Business\PlatformType;
use App\Models\Notification;
use App\Models\ShopRecord;
use Illuminate\Support\Facades\DB;

trait ServiceTrait
{
    protected function model($model,$user){
        if($user->data_permission){
            $department_arr = explode(',',$user->data_permission);
            if(count($department_arr)>0){
                return $model->whereIn('department_id',$department_arr);
            }
        }
        return $model;
    }
    //获取最后一个部门
    protected function getLastDepartment($request){
        if($request->filled('department_three_id')){
            return $request->department_three_id;
        }
        if($request->filled('department_two_id')){
            return $request->department_two_id;
        }
        if($request->filled('department_one_id')){
            return $request->department_one_id;
        }
        return 0;
    }
    protected function getUnread($shop_id,$time,$model){
        return $model
            ->where('shop_manage_id',$shop_id)
            ->where('is_read',1)
            ->where('operator_time',$time)
            ->count();
    }
    protected function yesterday(){
        $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
        return [date('Y-m-d H:i:s',$beginYesterday),date('Y-m-d H:i:s',$endYesterday)];
    }

    protected function towDays(){
        $beginYesterday=mktime(0,0,0,date('m'),date('d')-2,date('Y'));
        $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-2;
        return [date('Y-m-d H:i:s',$beginYesterday),date('Y-m-d H:i:s',$endYesterday)];
    }
    //七天
    protected function sevenDays(){
        $beginYesterday = mktime(0,0,0,date('m'),date('d')-7,date('Y'));
        return date('Y-m-d H:i:s',$beginYesterday);
    }
    protected function userIsReadAnnouncement($uid,$announce_id){
        $data = Notification::query()->where('data',$announce_id)
            ->where('notifiable_id',$uid)->value('read_at');
        if($data){
            return 1;
        }else{
            return 0;
        }
    }
    //获取方法
    protected function filterArray($res,$department){
        $shopUnread = [];
        foreach ($res as $k=>$v){
            $shopUnread[$k]['id'] = $v->id;
            $shopUnread[$k]['shop_name'] = $v->shop_name;
            $shopUnread[$k]['first'] = $v->department_one_id>0?$department[$v->department_one_id]:'';
            $shopUnread[$k]['two'] = $v->department_two_id>0?$department[$v->department_two_id]:'';
            $shopUnread[$k]['three'] = $v->department_three_id>0?$department[$v->department_three_id]:'';
            $shopUnread[$k]['un_read_num'] = $this->getUnread($v->shop_manage_id,$v->operator_time,new ShopRecord());
        }
        return $shopUnread;
    }
    //插入区间数据
    protected function  savePriceRange($id,$request){
        $data = $request->price_distance;
        if(is_array($data)){
            $arr = $data;
        }else{
            $arr = json_decode($data,true);
        }
        $map = [];
        foreach ($arr as $k=>$v){
            $map[$k]['group_id'] = $id;
            $map[$k]['min'] = $v['min'];
            $map[$k]['max'] = $v['max'];
            $map[$k]['price'] = $v['price'];
        }
        DB::table('price_ranges')->insert($map);
    }
    protected function findPlateformId(String $name){
        return PlatformType::query()->where('platform_name',$name)->value('id');
    }
}
